using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace libRDS
{
    public class LookUp
    {
        static LookUp m_This = new LookUp();
        public static LookUp Me{get{return m_This;}}
        Dictionary<DateTime, Dictionary<double,string> > m_mapCusipNotes = new Dictionary<DateTime,Dictionary<double,string>>();
        private LookUp()
        {
            fnLoadSecMst();
        }

        public string Find(double dCoupon, DateTime dtMaturityDate)
        {
            if(m_mapCusipNotes.ContainsKey(dtMaturityDate))
            {
                Dictionary<double, string> d = m_mapCusipNotes[dtMaturityDate];
                if(d.ContainsKey(dCoupon)) return d[dCoupon];
            }
            return null;
        }
        void fnLoadSecMst()
        {
            string sSQL = "SELECT id, maturity_date, coupon FROM Treasuries";
            string sConnStr = string.Format("Data Source={0};Initial Catalog={1};integrated security=SSPI;",
               "SERGEY-LP\\SQLDEVLP", "Sec");
            SqlConnection conn = new SqlConnection(sConnStr);
            SqlCommand cmd = new SqlCommand();
            conn.Open();
            cmd.Connection = conn;
            cmd.CommandText = sSQL;
            SqlDataReader dr = cmd.ExecuteReader();
            while (true)
            {
                if (!dr.Read()) break;
                string sCusip = dr.GetString(0);
                DateTime dtMat = dr.GetDateTime(1);
                double dCoupon = dr.GetDouble(2);
                if(!m_mapCusipNotes.ContainsKey(dtMat)) m_mapCusipNotes[dtMat]=new Dictionary<double,string>();
                m_mapCusipNotes[dtMat][dCoupon]=sCusip;
            }
            dr.Close();
            dr.Dispose();
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }
}
